home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / LIFER__ / PROTO / P / PD_LIFE_.C < prev    next >
Text File  |  1991-08-02  |  12KB  |  305 lines

  1. /* PD_LIFE_INPUT */
  2.  
  3. /* File name: LIFE_INPUT */
  4. /* Function: Handle a modal dialog */
  5. /* History: 7/27/91 Original by Prototyper 3.0   */
  6.  
  7.  
  8. #include "PCommonLife.h"    /* Common */
  9. #include "Common_Life.h"    /* Common */
  10. #include "PUtils_Life.h"    /* General Utilities */
  11. #include "Utils_Life.h"    /* General Utilities */
  12.  
  13. #include "PA_Life_Alert.h"    /* Alert */
  14. #include "PD_LIFE_INPUT.h"    /* This file */
  15. #include "LIFE_INPUT.h"    /* The user file */
  16.  
  17. /* ======================================================= */
  18.  
  19.  
  20. static Boolean        ExitDialog;                                             /* Flag used to exit the Dialog */
  21. static Point    MyPt;                                                         /* Current list selection point */
  22. static OSErr    MyErr;                                                      /* OS error returned */
  23. static DialogPtr    GetSelection;                                           /* Pointer to this dialog */
  24. static GrafPtr        SavedPort;                                            /* Previous grafport */
  25.  
  26. /* Prototypes */
  27. /* Filter routine for modal dialog routine */
  28. static pascal Boolean MyFilter (DialogPtr theDialog ,EventRecord *theEvent,short *itemHit);
  29.  
  30. /* This is an update routine for non-controls in the dialog */
  31. /* This is executed after the dialog is uncovered by an alert */
  32. static void Refresh_Dialog(void);                                         /* Refresh the dialogs non-controls */
  33.  
  34.  
  35.  
  36. /* ======================================================= */
  37.  
  38. /* Initialize the modal dialog, globals and call users routine */
  39.  
  40. void I_PD_LIFE_INPUT()
  41. {
  42.         Rect    tempRect;                                                     /* Temporary rectangle */
  43.         short    DType;                                                         /* Type of dialog item */
  44.         short    Index;                                                         /* For looping */
  45.         Handle    DItem;                                                        /* Handle to the dialog item */
  46.         ControlHandle    CItem, CTempItem;                                /* Control handle */
  47.         Str255    sTemp;                                                     /* Get text entered, temp holding */
  48.         short    itemHit;                                                       /* Get selection */
  49.         short    temp;                                                          /* Get selection, temp holding */
  50.  
  51.  
  52.  
  53.  
  54.         D_Init_LIFE_INPUT();
  55.  
  56. }                                                                                /* End of procedure */
  57.  
  58.  
  59. /* ======================================================= */
  60.  
  61. /* Filter routine for dialog events */
  62.  
  63. static pascal Boolean MyFilter (theDialog ,theEvent,itemHit)
  64. DialogPtr    theDialog;
  65. EventRecord        *theEvent;
  66. short        *itemHit;
  67. {
  68.         Point    MyPt;                                                         /* Current list selection point */
  69.         Rect    tempRect;                                                     /* Temporary rectangle */
  70.         short    DType;                                                         /* Type of dialog item */
  71.         Handle    DItem;                                                        /* Handle to the dialog item */
  72.         ControlHandle    CItem;                                               /* Control handle */
  73.         short    chCode;                                                        /* Key entered */
  74.         long    LTemp;                                                         /* Used for time delay */
  75.         char    MyCmdKey;                                                    /* The command key */
  76.         Boolean    CmdDown;                                                 /* Flag for command key used */
  77.         Boolean    valMyFilter;                                               /* Value to return */
  78.  
  79.  
  80.  
  81.         valMyFilter = D_Filter_LIFE_INPUT(theDialog, theEvent, itemHit);/* Call the user routine */
  82.  
  83.         if ((theEvent->what == updateEvt)  && ((WindowPtr)theEvent->message == theDialog))/* Only do on an update */
  84.             {
  85.                 BeginUpdate(theDialog);                                        /* Start the update */
  86.                 DrawDialog(theDialog);                                         /* Draw the controls */
  87.                 valMyFilter = TRUE;                                            /* Pass out this special itemHit number */
  88.                 *itemHit = 32000;                                             /* Our special ID */
  89.             }
  90.         if (theEvent->what == mouseDown)                                /* Only do on a mouse click */
  91.             {
  92.                 MyPt = theEvent->where;                                     /* Get the point where the mouse was clicked */
  93.                 GlobalToLocal(&MyPt);                                        /* Convert global to local */
  94.  
  95.  
  96.             }
  97.         if (theEvent->what == keyDown)
  98.             {
  99.                 chCode =  theEvent->message & charCodeMask;           /* Get character */
  100.                 MyCmdKey = (char)chCode;                                    /* Change to ASCII */
  101.                 CmdDown = ((theEvent->modifiers / cmdKey) != 0);/* Get command key state */
  102.                 if (CmdDown == TRUE)                                         /* Do if command key was down */
  103.                     {
  104.                         if ((MyCmdKey == 'x') || (MyCmdKey == 'X'))
  105.                             {
  106.                                 DlgCut(theDialog);
  107.                                 valMyFilter = TRUE;
  108.                             }
  109.                         else if ((MyCmdKey =='c') || (MyCmdKey == 'C')) 
  110.                             {
  111.                                 DlgCopy(theDialog);
  112.                                 valMyFilter = TRUE;
  113.                             }
  114.                         else if ((MyCmdKey =='v') || (MyCmdKey == 'V'))
  115.                             {
  116.                                 DlgPaste(theDialog);
  117.                                 valMyFilter = TRUE;
  118.                             }
  119.                     }
  120.                 else if ((chCode == 13) || (chCode == 3))                    /* CR or Enter */
  121.                     {
  122.                         valMyFilter = TRUE;                                     /* Flag we got a hit */
  123.                         *itemHit = 1;                                             /* Default for CR */
  124.                         GetDItem (theDialog ,*itemHit, &DType, &DItem, &tempRect);/* Get the item */
  125.                         if (DType == (ctrlItem + btnCtrl))                     /* If a button then ... */
  126.                             {
  127.                                 CItem = (ControlHandle)DItem;                  /* Make it a controlhandle */
  128.                                 HiliteControl(CItem, 10);                         /* Hilite it */
  129.                                 LTemp = TickCount() + 15;                       /* Flash the button for 1/4 second */
  130.                                 do
  131.                                 {}
  132.                                 while (LTemp > TickCount());
  133.                                 HiliteControl(CItem, 0);                          /* UnHilite it */
  134.                             }
  135.                     }
  136.             }
  137.  
  138.         return(valMyFilter);
  139. }
  140.  
  141. /* ======================================================= */
  142.  
  143.  
  144.  
  145. /* ======================================================= */
  146.  
  147. /* This is an update routine for non-controls in the dialog */
  148. /* This is executed after the dialog is uncovered by an alert */
  149. static void Refresh_Dialog()                                               /* Refresh the dialogs non-controls */
  150.         Rect    rTempRect;                                                    /* Temp rectangle used for drawing */
  151.         short    DType;                                                         /* Type of dialog item */
  152.         Handle    DItem;                                                        /* Handle to the dialog item */
  153.         ControlHandle    CItem;                                               /* Control handle */
  154.  
  155.  
  156.  
  157.         SetPort(GetSelection);                                               /* Point to our dialog window */
  158.         rTempRect = tempRect;                                              /* Save the current contents of  tempRect */
  159.         GetDItem(GetSelection,Res_Dlg_NEXT,&DType,&DItem,&tempRect);/* Get the item handle */
  160.         PenSize(3, 3);                                                         /* Change pen to draw thick default outline */
  161.         InsetRect(&tempRect, -4, -4);                                     /* Draw outside the button by 1 pixel */
  162.         FrameRoundRect(&tempRect, 16, 16);                            /* Draw the outline */
  163.         PenSize(1, 1);                                                         /* Restore the pen size to the default value */
  164.  
  165.         /* Draw a line, Drawn line2  */
  166.         PenSize(3,3);                                                         /* Set a wider pen width */
  167.         MoveTo(0,20);                                                        /* Horz,vert, Move to starting position */
  168.         LineTo(299,20);                                                      /* Horz,vert, Draw to the ending position */
  169.         PenSize(1,1);                                                         /* Back to default pen size */
  170.  
  171.         /* Draw a line, Drawn line2-3  */
  172.         PenSize(3,3);                                                         /* Set a wider pen width */
  173.         MoveTo(0,70);                                                        /* Horz,vert, Move to starting position */
  174.         LineTo(299,70);                                                      /* Horz,vert, Draw to the ending position */
  175.         PenSize(1,1);                                                         /* Back to default pen size */
  176.  
  177.         tempRect = rTempRect;                                              /* Restore the current contents of  tempRect */
  178.         TextSize(10);
  179.         TextFont(courier);                                                    /* Select the Font that we want */
  180.         /* Draw a string of text, Static Text  */
  181.         SetRect(&rTempRect, 0,0,300,15);
  182.         GetIndString(sTemp,Res_Dlg_Static_Text6,1);                 /* Get the string */
  183.         TextBox(&sTemp[1], sTemp[0], &rTempRect,teJustLeft);
  184.  
  185.         TextSize(12);
  186.         TextFont(systemFont);                                               /* Select the Font that we want */
  187.         TextFace(0);                                                           /* Select the style that we want */
  188.  
  189.  
  190.         D_Refresh_LIFE_INPUT(GetSelection);                             /* Call user refresh routine */
  191.  
  192. }
  193.  
  194. /* ======================================================= */
  195.  
  196.  
  197. void PD_LIFE_INPUT()
  198. {
  199.         Rect    tempRect;                                                     /* Temporary rectangle */
  200.         short    DType;                                                         /* Type of dialog item */
  201.         short    Index;                                                         /* For looping */
  202.         Handle    DItem;                                                        /* Handle to the dialog item */
  203.         ControlHandle    CItem;                                               /* Control handle */
  204.         ControlHandle    CTempItem;                                         /* temp Control handle */
  205.         Str255    sTemp;                                                     /* Get text entered, temp holding */
  206.         short    itemHit;                                                       /* Get selection */
  207.         short    temp;                                                          /* Get selection, temp holding */
  208.         TEHandle    ThisEditText;                                             /* Handle to get the Dialogs TE record */
  209.         DialogPeek        TheDialogPtr;                                       /* Pointer to Dialogs definition record, contains the TE record */
  210.         Boolean        good_values = 1;    /* FLAG USED TO CHECK IF VALUES FOR X & Y ARE GOOD */
  211.     
  212.         GetPort(&SavedPort);               /* Get the previous grafport */
  213.  
  214.  
  215.         GetSelection = GetNewDialog(Res_D_LIFE_INPUT, NIL,  (Ptr)-1);/* Bring in the dialog resource */
  216.         tempRect = GetSelection->portRect;                               /* Get window size, we will now center it */
  217.         tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;/* Center vert */
  218.         tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;/* Center Horz */
  219.         MoveWindow(GetSelection, tempRect.left, tempRect.top, TRUE);/* Now move the window to the proper position */
  220.         ShowWindow(GetSelection);                                         /* Open a dialog box */
  221.         SelectWindow(GetSelection);                                        /* Lets see it */
  222.         SetPort(GetSelection);                                               /* Prepare to add conditional text */
  223.  
  224.         TheDialogPtr = (DialogPeek)GetSelection;                         /* Get to the inner record */
  225.         ThisEditText = TheDialogPtr->textH;                              /* Get to the TE record */
  226.         HLock((Handle)ThisEditText);                                       /* Lock it for safety */
  227.         (*ThisEditText)->txSize = 12;                                     /* TE Point size */
  228.         TextSize(12);                                                         /* Window Point size */
  229.         (*ThisEditText)->txFont = systemFont;                           /* TE Font ID */
  230.         TextFont(systemFont);                                               /* Window Font ID */
  231.         (*ThisEditText)->txFont = 0;                                       /* TE Font ID */
  232.         (*ThisEditText)->fontAscent = 12;                                /* Font ascent */
  233.         (*ThisEditText)->lineHeight = 12 + 3 + 1;                        /* Font ascent + descent + leading */
  234.         HUnlock((Handle)ThisEditText);                                     /* UnLock the handle when done */
  235.  
  236.  
  237.         /* Setup initial conditions */
  238.         Refresh_Dialog();                                                      /* Draw any Lists, lines, or rectangles */
  239.  
  240.         ExitDialog = FALSE;                                                   /* Do not exit dialog handle loop yet */
  241.  
  242.         D_Setup_LIFE_INPUT(GetSelection);                               /* Call user Dialog setup routine */
  243.  
  244.         do                                                                        /* Start of dialog handle loop */
  245.         {
  246.                 ModalDialog(&MyFilter, &itemHit);                         /* Wait until an item is hit */
  247.  
  248.                 if (itemHit == 32000)                                          /* Check for update */
  249.                     {
  250.                         Refresh_Dialog();                                         /* Draw any Lists, lines, or rectangles*/
  251.                         EndUpdate(GetSelection);                                /* End of the update*/
  252.                     }
  253.                 else
  254.                     {    
  255.                     GetDItem(GetSelection, itemHit, &DType, &DItem, &tempRect);
  256.                     }
  257.  
  258.                 D_Hit_LIFE_INPUT(GetSelection,itemHit,&ExitDialog);/* Let user handle the item hit */
  259.  
  260.                 /* Handle it real time */
  261.                 if (itemHit ==Res_Dlg_NEXT)                                 /* Handle the Button being pressed */
  262.                     {
  263.                         /* CHECK VALUES OF X & Y */
  264.                         good_values = CHECKXY(GetSelection, DType, DItem, tempRect);
  265.                         if (good_values)
  266.                         {
  267.                             STORE_VALUES(GetSelection, DType, DItem, tempRect);
  268.                         }
  269.                     }
  270.  
  271.                 if (itemHit ==Res_Dlg_Cancel)                                /* Handle the Button being pressed */
  272.                     {
  273.                         /* QUIT PROGRAM */
  274.                         ExitDialog = TRUE;
  275.                         doneFlag = TRUE;                                       /* Close this dialog, exit */
  276.                     }
  277.  
  278.                 if (itemHit ==Res_Dlg_OK2)                                  /* Handle the Button being pressed */
  279.                     {
  280.                         /* CHECK VALUES OF X & Y */
  281.                         good_values = CHECKXY(GetSelection, DType, DItem, tempRect);
  282.                         if (good_values)
  283.                         {
  284.                             STORE_VALUES(GetSelection, DType, DItem, tempRect);
  285.                             ExitDialog = TRUE; 
  286.                         
  287.                             /* BRING UP DISPLAY WINDOW */
  288.                             Open_LIFE_WINDOW(); 
  289.                         
  290.                             /* DISPLAY & COMPUTE GENERATION */
  291.                             LIFE_PROGRAM();
  292.                         }
  293.                     }
  294.         }
  295.         while (ExitDialog == FALSE);                                        /* Handle dialog items until exit selected */
  296.  
  297.         D_Exit_LIFE_INPUT(GetSelection);                                 /* Exiting the modal dialog */
  298.  
  299.         SetPort(SavedPort);                                                 /* Restore the previous grafport */
  300.  
  301.         DisposDialog(GetSelection);                                         /* Flush the dialog out of memory */
  302.  
  303.  
  304. }                                                                                /* End of procedure */